package me.yiyou.meihua.ui.page.random

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import me.yiyou.meihua.data.BaGuaImage
import me.yiyou.meihua.data.SaveBean
import me.yiyou.meihua.ui.page.dateqiuga.GetSingleYao
import me.yiyou.meihua.ui.page.dateqiuga.getBianGuaImageMethod
import me.yiyou.meihua.ui.page.dateqiuga.getHuGuaImageMethod
import me.yiyou.meihua.utils.getGuaNameByImgShang
import me.yiyou.meihua.utils.getGuaNameByImgXia

@Composable
fun RandomPage(viewModel: RandomViewModel = viewModel(), saveBean: SaveBean) {

    var baGuaImage: BaGuaImage? = null
    var huGuaImage: BaGuaImage? = null
    var bianGuaImage: BaGuaImage? = null

    var shangGua = saveBean.shang
    var xiaGua = saveBean.xia
    var dongYao = saveBean.dong
    var content = saveBean.content
    var time = saveBean.time

    LazyColumn(Modifier.fillMaxWidth()) {

        item {
            // 卦象部分
            Row(
                Modifier
                    .fillMaxWidth()
                    .padding(top = 60.dp, start = 10.dp, end = 10.dp),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically
            ) {
                // 本卦部分
                Column(Modifier.width(100.dp)) {
                    baGuaImage = viewModel.paintGua(shangGua, xiaGua, dongYao)
                    if (dongYao == 6) GetSingleYao(
                        baGuaImage!!.six, true
                    ) else GetSingleYao(baGuaImage!!.six)

                    if (dongYao == 5) GetSingleYao(
                        baGuaImage!!.five, true
                    ) else GetSingleYao(baGuaImage!!.five)

                    if (dongYao == 4) GetSingleYao(
                        baGuaImage!!.four, true
                    ) else GetSingleYao(baGuaImage!!.four)

                    if (dongYao == 3) GetSingleYao(
                        baGuaImage!!.three, true
                    ) else GetSingleYao(
                        baGuaImage!!.three
                    )

                    if (dongYao == 2) GetSingleYao(
                        baGuaImage!!.two, true
                    ) else GetSingleYao(baGuaImage!!.two)

                    if (dongYao == 1) GetSingleYao(
                        baGuaImage!!.one, true
                    ) else GetSingleYao(baGuaImage!!.one)
                }
                // 互卦
                Column(Modifier.width(100.dp)) {
                    huGuaImage = getHuGuaImageMethod(baGuaImage!!)
                    GetSingleYao(huGuaImage!!.six)
                    GetSingleYao(huGuaImage!!.five)
                    GetSingleYao(huGuaImage!!.four)
                    GetSingleYao(huGuaImage!!.three)
                    GetSingleYao(huGuaImage!!.two)
                    GetSingleYao(huGuaImage!!.one)
                }
                // 变卦
                Column(Modifier.width(100.dp)) {
                    bianGuaImage = getBianGuaImageMethod(baGuaImage!!, dongYao)
                    GetSingleYao(bianGuaImage!!.six)
                    GetSingleYao(bianGuaImage!!.five)
                    GetSingleYao(bianGuaImage!!.four)
                    GetSingleYao(bianGuaImage!!.three)
                    GetSingleYao(bianGuaImage!!.two)
                    GetSingleYao(bianGuaImage!!.one)
                }
            }

            // 分析部分
            Column(
                Modifier
                    .fillMaxWidth()
                    .padding(top = 20.dp, start = 10.dp, end = 10.dp),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ) {
                Text(
                    text = "本卦: ${getGuaNameByImgShang(baGuaImage!!)} + ${
                        getGuaNameByImgXia(
                            baGuaImage!!
                        )
                    } + ${dongYao}爻动 ", fontSize = 18.sp
                )
                Text(
                    text = "互卦: ${getGuaNameByImgShang(huGuaImage!!)} + ${
                        getGuaNameByImgXia(
                            huGuaImage!!
                        )
                    } ", fontSize = 18.sp
                )
                Text(
                    text = "变卦: ${getGuaNameByImgShang(bianGuaImage!!)} + ${
                        getGuaNameByImgXia(
                            bianGuaImage!!
                        )
                    } ", fontSize = 18.sp
                )
            }

            // 时间部分
            Column(
                Modifier
                    .fillMaxWidth()
                    .padding(top = 20.dp, start = 10.dp, end = 10.dp)
            ) {
                Text(text = "起卦时间: ${time}", fontSize = 16.sp, color = Color.Gray)
            Text(text = "占问事: ${content}", fontSize = 16.sp, color = Color.Gray)
            }
        }
    }
}